<?php
/**
 * gd.php
 *
 * @copyright  2017 opencart.cn - All Rights Reserved
 * @link       http://www.guangdawangluo.com
 * @author     Edward Yang <yangjin@opencart.cn>
 * @created    2/23/17 10:32
 * @modified   2/23/17 10:32
 */

/**
 * Deprecated, please use array_get
 * Example:
 * $css = array(
 *   'border' => array(
 *     'top' => array(
 *       'size' => '5px'
 *     )
 *   )
 * );
 * $size = gdvalue($css, 'border.top.size');   // 5px;
 * $color = gdvalue($css, 'border.top.color', '#FFF');   // #FFF;
 *
 * @param $array
 * @param $key
 * @param null $default
 * @return null
 */

if (!function_exists('gdValue')) {
    /**
     * @param $array
     * @param $key
     * @param null $default
     * @return null
     */
    function gdValue($array, $key, $default = null)
    {
        return array_get($array, $key, $default);
    }
}
if (!function_exists('array_get')) {
    /**
     * @param $array
     * @param $key
     * @param null $default
     * @return null
     */
    function array_get($array, $key, $default = null)
    {
        if (!is_array($array)) {
            return $default;
        }

        $key = explode('.', $key);
        foreach ($key as $value) {
            if (!is_array($array)) {
                return $default;
            }
            if (array_key_exists($value, $array) === false) {
                return $default;
            }
            $array = $array[$value];
        }
        return $array;
    }
}

if (!function_exists('studlyCase')) {
    /**
     * Convert a value to studly caps case.
     *
     * @param  string $value
     * @return string
     */
    function studlyCase($value)
    {
        return ucwords(str_replace(['-', '_'], ' ', $value));
    }
}

// OpenCart version type check
if (!function_exists('isOCPro')) {
    function isOCPro()
    {
        return (defined('OCTYPE') && OCTYPE == 'PRO');
    }
}

if (!function_exists('isOCFree')) {
    function isOCFree()
    {
        return (defined('OCTYPE') && OCTYPE == 'FREE');
    }
}


if (!function_exists('renderCsv')) {
    /**
     * Convert a value to studly caps case.
     *
     * @param  string $value
     * @return string
     */
    function renderCsv($value)
    {
        $filename = date('Ymd') . '.csv';
        header("Content-type:text/csv");
        header("Content-Disposition:attachment;filename=" . $filename);
        header('Cache-Control:must-revalidate,post-check=0,pre-check=0');
        header('Expires:0');
        header('Pragma:public');
        echo $value;
        exit;
    }
}

if (!function_exists('isAdmin')) {
    /**
     * Check current is admin or catalog
     *
     * @return string
     */
    function isAdmin()
    {
        return defined('HTTPS_CATALOG');
    }
}

if (!function_exists('gdSubStr')) {
    /**
     * @param $string
     * @param $length
     * @param string $dot
     * @return string
     */
    function gdSubStr($string, $length, $dot = '...')
    {
        $strLength = strlen($string);
        if ($length <= 0) {
            return $string;
        } elseif ($strLength <= $length) {
            return $string;
        }
        return utf8_substr($string, 0, $length) . $dot;
    }
}

function gdDate($format, $timestamp = null)
{
    //$format = 'Y-m-d H:i:s';
    $timestamp ?: time();
    $gdDatetime = new Guangda\Datetime();
    $baseDatetime = date('Y-m-d H:i:s', $timestamp);
    $gbDatetime = $gdDatetime->convert($baseDatetime, '', $format);
    return $gbDatetime;
}
